
package com.yuke.cloud.service.tmc.config;


import com.yuke.cloud.common.base.constant.MQConstants;
import com.yuke.cloud.service.tmc.mq.TmcMessageListener;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration

/*@ComponentScan(basePackageClasses={RabbitSender.class, RedisConfiguration.class,YukeCoreConfig.class})*/

//todo 用于rabbitmq相关
public class TmcQueueConfig {


    @Bean
    public DirectExchange businessExchange() {
        DirectExchange directExchange = new DirectExchange(MQConstants.BUSINESS_EXCHANGE);
        return directExchange;
    }


    @Bean
    public Queue wmcQueues() {
        Map<String, Object> arguments = new HashMap<String, Object>();


        arguments.put("x-dead-letter-exchange", MQConstants.DLX_EXCHANGE);
        arguments.put("x-dead-letter-routing-key", MQConstants.DLX_ROUTING_KEY);
        //arguments.put("x-message-ttl", 60000);
        //arguments.put("x-max-length", 300);
        return new Queue(MQConstants.BUSINESS_QUEUE_TMC, true, false, false, arguments);
    }


    @Bean
    public Binding opcBinding() {
        return BindingBuilder.bind(wmcQueues()).to(businessExchange())
                .with(MQConstants.BUSINESS_KEY_TMC);
    }

    @Bean
    public SimpleMessageListenerContainer bizListenerContainer(ConnectionFactory cn,
                                                               TmcMessageListener wmc) {
        SimpleMessageListenerContainer containers = new SimpleMessageListenerContainer(cn);
        containers.setQueues(wmcQueues());
        containers.setExposeListenerChannel(true);
        containers.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        containers.setMessageListener(wmc);
        containers.setPrefetchCount(10);
        return containers;
    }


}

